package cn.com.acca.ma.dao.impl;

import cn.com.acca.ma.dao.StockIndexMonthDao;
import cn.com.acca.ma.hibernate.util.HibernateUtil;
import cn.com.acca.ma.jpa.util.JpaUtil;
import cn.com.acca.ma.model.StockIndexMonth;
import cn.com.acca.ma.mybatis.util.MyBatisUtil;
import org.hibernate.Query;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StockIndexMonthDaoImpl extends BaseDaoImpl<StockIndexMonthDaoImpl> implements StockIndexMonthDao {

    /**************************************************************************************************
     *
     * 									       更新周线级别，所有月份的指数数据
     *
     **************************************************************************************************/
    /**
     * 计算月线级别，所有指数的基础数据
     */
    @Override
    public void writeStockIndexMonth() {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonth");
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**
     * 计算月线级别，所有指数的移动平均线
     */
    @Override
    public void writeStockIndexMonthMA() {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMA5");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMA10");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMA20");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMA60");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMA120");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMA250");
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**
     * 计算月线级别，所有指数的KD
     */
    @Override
    public void writeStockIndexMonthKD() {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthKD_init");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthKD_rsv");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthKD_k");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthKD_d");
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**
     * 计算月线级别，所有指数的MACD
     */
    @Override
    public void writeStockIndexMonthMACD() {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACD_init");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACD_ema");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACD_dif");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACD_dea");
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACD_macd");
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**************************************************************************************************
     *
     * 									       更新月线级别，某一月的指数数据
     *
     **************************************************************************************************/
    /**
     * 根据开始时间和结束时间，计算某一个月的所有指数的基础数据
     * @param beginDate
     * @param endDate
     */
    @Override
    public void writeStockIndexMonthByDate(String beginDate, String endDate) {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        Map<String, String> map = new HashMap<String, String>();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthByDate", map);
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**
     * 根据每个月的开始时间和结束时间，计算某一个月的所有指数的移动平均线
     * @param currentMonthBeginDate
     * @param currentMonthEndDate
     */
    @Override
    public void writeStockIndexMonthMAByDate(String currentMonthBeginDate, String currentMonthEndDate) {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        Map<String, String> map = new HashMap<String, String>();
        map.put("currentMonthBeginDate", currentMonthBeginDate);
        map.put("currentMonthEndDate", currentMonthEndDate);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMAByDate", map);
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**
     * 根据开始时间和结束时间，计算某一个月的所有指数的kd
     * @param beginDate
     * @param endDate
     */
    @Override
    public void writeStockIndexMonthKDByDate(String beginDate, String endDate) {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        Map<String, String> map = new HashMap<String, String>();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthKDByDate_rsv", map);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthKDByDate_k", map);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthKDByDate_d", map);
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**
     * 根据开始时间和结束时间，计算某一个月的所有指数的MACD
     * @param beginDate
     * @param endDate
     */
    @Override
    public void writeStockIndexMonthMACDByDate(String beginDate, String endDate) {
        sqlSession = MyBatisUtil.openSqlSession();// 打开会话，事务开始;
        Map<String, String> map = new HashMap<String, String>();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACDByDate_ema", map);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACDByDate_dif", map);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACDByDate_dea", map);
        sqlSession.insert("cn.com.acca.ma.xmlmapper.StockIndexMonthMapper.writeStockIndexMonthMACDByDate_macd", map);
        sqlSession.commit();// 提交会话，即事务提交
        sqlSession.close();// 关闭会话，释放资源
    }

    /**
     * 根据开始时间beginDate和结束时间endDate，从表STOCK_INDEX_MONTH中获取END_DATE字段
     * @param beginDate
     * @param endDate
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Date> getDateByCondition(String beginDate, String endDate){
        logger.info("根据开始时间【" + beginDate + "】和结束时间【" + endDate + "】，"
            + "从表STOCK_INDEX_MONTH中获取END_DATE字段");

        StringBuffer hql = new StringBuffer("select distinct t.endDate from StockIndexMonth t " +
            "where t.endDate between to_date(?,'yyyy-mm-dd') and to_date(?,'yyyy-mm-dd')");
        session = HibernateUtil.currentSession();
        session.beginTransaction();
        Query query = session.createQuery(hql.toString());
        query.setParameter(0, beginDate);
        query.setParameter(1, endDate);
        List<Date> list = query.list();
        session.getTransaction().commit();
        session.close();

        return list;
    }

    /**
     * 从表STOCK_INDEX_MONTH中获取date日的所有StockIndexMonth对象
     * @param date
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<StockIndexMonth> getStockIndexMonthByDate(String date){
        logger.info("从表STOCK_INDEX_MONTH中获取日期【" + date + "】的所有StockIndexMonth对象");

        StringBuffer hql = new StringBuffer("select t from StockIndexMonth t " +
            "where t.endDate=to_date(?,'yyyy-mm-dd') order by t.endDate asc");
        session = HibernateUtil.currentSession();
        session.beginTransaction();
        Query query = session.createQuery(hql.toString());
        query.setParameter(0, date);
        List<StockIndexMonth> list=query.list();
        session.getTransaction().commit();
        session.close();

        return list;
    }

    /**
     * 插入StockIndexMonth对象
     * @param stockIndexXMonth
     */
    public void saveStockIndexMonth(StockIndexMonth stockIndexXMonth){
        logger.info("插入StockIndexMonth对象");

        em = JpaUtil.currentEntityManager();
        em.getTransaction().begin();
        em.persist(stockIndexXMonth);
        em.getTransaction().commit();
        em.close();
    }
}
